package panels;

import conn.EMIS4Connection;
import dao.LoginDao;
import dao.PersonDao;
import exec.NotFoundException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import obj.Login;
import util.LoggedInUser;
import util.PersonRequested;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DeletePatientPanel.java
 *
 * Created on Aug 14, 2011, 7:09:34 PM
 */
/**
 *
 * @author hernandez
 */
public class DeletePatientPanel extends javax.swing.JPanel {

    LoggedInUser lUser = LoggedInUser.getInstance();
    PersonRequested rPerson = PersonRequested.getInstance();
    

    /** Creates new form DeletePatientPanel */
    public DeletePatientPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    deletePatientLabel = new javax.swing.JLabel();
    submitButton = new javax.swing.JButton();
    agreeCheckbox = new javax.swing.JCheckBox();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setBackground(new java.awt.Color(220, 220, 210));

    deletePatientLabel.setFont(new java.awt.Font("Tahoma", 1, 24));
    deletePatientLabel.setText("Delete Patient");
    deletePatientLabel.setName("deletePatientLabel"); // NOI18N

    submitButton.setText("Submit");
    submitButton.setName("submitButton"); // NOI18N
    submitButton.setOpaque(false);
    submitButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        submitButtonActionPerformed(evt);
      }
    });

    agreeCheckbox.setText("I AGREE");
    agreeCheckbox.setName("agreeCheckbox"); // NOI18N
    agreeCheckbox.setOpaque(false);

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    jTextArea1.setBackground(new java.awt.Color(204, 204, 204));
    jTextArea1.setColumns(20);
    jTextArea1.setEditable(false);
    jTextArea1.setLineWrap(true);
    jTextArea1.setRows(5);
    jTextArea1.setText("WARNING: You will NOT be able to review this patient's records after deletion!\n\n+ DO NOT delete without proper authorization.\n\n+ The patient's access will be locked!\n\n+ Your actions will be tracked!\n\nCheck 'I AGREE' to delete this patient.");
    jTextArea1.setName("jTextArea1"); // NOI18N
    jScrollPane1.setViewportView(jTextArea1);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(layout.createSequentialGroup()
            .add(12, 12, 12)
            .add(deletePatientLabel))
          .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .add(agreeCheckbox)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 434, Short.MAX_VALUE)
            .add(submitButton))
          .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .add(20, 20, 20)
        .add(deletePatientLabel)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 202, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(submitButton)
          .add(agreeCheckbox))
        .addContainerGap(18, Short.MAX_VALUE))
    );
  }// </editor-fold>//GEN-END:initComponents

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
// TODO add your handling code here:
    if (lUser != null && rPerson.getPerson() != null) {
        if (lUser.getUserID() != rPerson.getPerson().getPersonID()) {
            if (agreeCheckbox.isSelected()) {
                LoginDao login = new LoginDao();
                PersonDao personDao = new PersonDao();
                EMIS4Connection conn = EMIS4Connection.getInstance();
                rPerson.getPerson().setActiveStatus(false);

                try {
                    Login key = login.getObject(conn.getConnection(), rPerson.getPerson().getPersonID());
                    key.setLocked(true);
                    login.save(conn.getConnection(), key);
                } catch (NotFoundException ex) {
                    Logger.getLogger(DeletePatientPanel.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(DeletePatientPanel.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                    personDao.save(conn.getConnection(), rPerson.getPerson());
                } catch (NotFoundException ex) {
                    Logger.getLogger(DeletePatientPanel.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(DeletePatientPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
                JOptionPane.showMessageDialog(this, "Lock and Delete Successful.");
            }
        }
    } else {
        JOptionPane.showMessageDialog(this, "You must search for a patient first.");
    }

}//GEN-LAST:event_submitButtonActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JCheckBox agreeCheckbox;
  private javax.swing.JLabel deletePatientLabel;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextArea jTextArea1;
  private javax.swing.JButton submitButton;
  // End of variables declaration//GEN-END:variables
}
